﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DotNetNuke.Entities.Content;
using DotNetNuke.Services.Localization;
using DotNetNuke.Entities.Modules;
using iLangl.Model;
using iLangl.Customer.DAL;
using DotNetNuke.Entities.Tabs;

namespace iLangl.Customer.DNN
{
	public class TranslationStateManager
	{
		#region Singelton Pattern


		private static TranslationStateManager instance;
		private static object mutex = new object();
		/// <summary>
		/// The instance of the TranslationStateManager.
		/// </summary>
		public static TranslationStateManager Instance
		{
			get
			{
				if (instance == null)
				{
					lock (mutex)
					{
						if (instance == null)
						{
							instance = new TranslationStateManager();

						}
					}
				}
				return instance;
			}
		}
		#endregion

		public TranslationState GetState(TabInfo tabmodule, Locale source, Locale target)
		{
			String sourcecustomerIdentity = IdentityManager.GetCustomerIdentity(tabmodule);
			var tController = new TabController();
			TabInfo targetTab = tController.GetTabByCulture(tabmodule.TabID, tabmodule.PortalID, target);
			String targetcustomerIdentity = IdentityManager.GetCustomerIdentity(tabmodule);

			return getState(
				sourcecustomerIdentity,
				targetcustomerIdentity,
				source,
				target,
				VersionManager.GetVersion(tabmodule),
				VersionManager.GetVersion(targetTab));
		}

		public TranslationState GetState(ModuleInfo sourcemodule, Locale source, Locale target)
		{
			String sourcecustomerIdentity = IdentityManager.GetCustomerIdentity(sourcemodule);
			//Get Target Module
			var mController = new ModuleController();
			ModuleInfo targetmodule = mController.GetModuleByCulture(sourcemodule.ModuleID, sourcemodule.TabID, sourcemodule.PortalID, target);
			String targetcustomerIdentity = IdentityManager.GetCustomerIdentity(targetmodule);

			return getState(
				sourcecustomerIdentity, 
				targetcustomerIdentity, 
				source, 
				target,
				VersionManager.GetVersion(sourcemodule),
				VersionManager.GetVersion(targetmodule));
		}

		private TranslationState getState(
			string sourcecustomerIdentity, 
			string targetcustomerIdentity, 
			Locale source, 
			Locale target,
			String sourceversion,
			String targetversion)
		{
			TranslationState result = new TranslationState();
				
			result.Status |= eTranslationState.Nothing;
				 

			Language sourcelanguage = LanguageManager.LocaleToLanguage(source);
			Language targetlanguage = LanguageManager.LocaleToLanguage(target);

			//Looking for translations
			IList<Translation> translations = TranslationManager.Instance.FindTranslations(
				sourcecustomerIdentity,
				targetcustomerIdentity,
				sourcelanguage.LanguageId,
				targetlanguage.LanguageId);


			//Check if ordered or translated
			foreach (Translation translation in translations)
			{
				ProjectStatuses.eStatus pStatus = ProjectStatuses.GetStatus(translation.Project.Status);

				if (pStatus == ProjectStatuses.eStatus.InProgress)
				{
					result.Status |= eTranslationState.Ordered;

					if (translation.Project is CustomerProject)
					{
						if ((translation.Project as CustomerProject).IsLockContentDuringTranslation)
						{
							result.Status |= eTranslationState.Locked;
						}
					}
				}

				if ((pStatus == ProjectStatuses.eStatus.Archived) || (pStatus == ProjectStatuses.eStatus.Finished))
				{
					result.Status |= eTranslationState.Translated;
				}

			}

			//Analize Source and Target Changed
			bool isSourceChanged = true;
			bool isTargetChanged = true;
			if
			(
				((result.Status & eTranslationState.Ordered) == eTranslationState.Ordered)
				||
				((result.Status & eTranslationState.Translated) == eTranslationState.Translated)
			)
			{
				//Check if ordered or translated
				foreach (Translation translation in translations)
				{
					if(translation.SourceContent != null)
					{
						ContentDescription cd = DALFacade.Instance.ContentDAL.ReadContentDescription(translation.SourceContent.Id);

						if(cd.Version == sourceversion)
						{
							isSourceChanged = false;
						}
					}

					if(translation.TargetContent != null)
					{
						ContentDescription cd = DALFacade.Instance.ContentDAL.ReadContentDescription(translation.TargetContent.Id);

						if (cd.Version == targetversion)
						{
							isTargetChanged = false;
						}

					}
				}
			}

			if (isSourceChanged)
			{
				result.Status |= eTranslationState.SourceChanged;
			}

			if (isTargetChanged)
			{
				result.Status |= eTranslationState.TargetChanged;
			}

			return result;
		}
	}
}